package com.qf.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.crypto.interfaces.PBEKey;


/**
 * 密码编码器
 */
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder(){
        System.out.println(2222);
        return new BCryptPasswordEncoder();
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //放开跨站访问控制 + 允许帧标签展示内容
        http.csrf().disable().headers().frameOptions().sameOrigin();

        http.authorizeRequests()
                //哪些资源不需要登录
                .antMatchers("/css/**","/js/**","/img/**","/plugins/**","/login.html").permitAll()
                //哪些资源需要登录
                .antMatchers("/**").authenticated();

        //配置访问权限

        //没有登录，跳转到登录页面
        http.formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/login")
                .permitAll();

        //如果没有权限，跳转到无权限页面
        http.exceptionHandling().accessDeniedPage("/");
    }
}
